package xkp.lesson4;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class TestWriteChannel {
    public static void main(String[] args) throws IOException {
        String str = "hello 你好:"+System.currentTimeMillis();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put(str.getBytes());
        FileChannel channel = new RandomAccessFile(
                "Lesson30/file/newFile","rw").getChannel();
        //把buffer中的数据读取出来，写入通道中
        //转换buffer模式
        buffer.flip();
        channel.write(buffer);
        channel.force(false);
        channel.close();
    }
}
